/*
 * @Author: szx
 * @Date: 2022-03-09 20:49:04
 * @LastEditTime: 2022-03-09 20:51:04
 * @Description:
 * @FilePath: \MDN_JS学习\测试\Promise错误示例.js
 */
'use strict';

// To experiment with error handling, "threshold" values cause errors randomly
const THRESHOLD_A = 8; // can use zero 0 to guarantee error

function tetheredGetNumber(resolve, reject) {
    try {
        setTimeout(function () {
            const randomInt = Date.now();
            const value = randomInt % 10;
            try {
                if (value >= THRESHOLD_A) {
                    throw new Error(`Too large: ${value}`);
                }
            } catch (msg) {
                reject(`Error in callback ${msg}`);
            }
            resolve(value);
            return;
        }, 500);
        // To experiment with error at set-up, uncomment the following 'throw'.
        // throw new Error("Bad setup");
    } catch (err) {
        reject(`Error during setup: ${err}`);
    }
    return;
}

function determineParity(value) {
    const isOdd = value % 2 ? true : false;
    const parityInfo = { theNumber: value, isOdd: isOdd };
    return parityInfo;
}

function troubleWithGetNumber(reason) {
    console.error(`Trouble getting number: ${reason}`);
    throw -999; // must "throw" something, to maintain error state down the chain
}

function promiseGetWord(parityInfo) {
    // The "tetheredGetWord()" function gets "parityInfo" as closure variable.
    const tetheredGetWord = function (resolve, reject) {
        const theNumber = parityInfo.theNumber;
        const threshold_B = THRESHOLD_A - 1;
        if (theNumber >= threshold_B) {
            reject(`Still too large: ${theNumber}`);
        } else {
            parityInfo.wordEvenOdd = parityInfo.isOdd ? 'odd' : 'even';
            resolve(parityInfo);
        }
        return;
    };
    return new Promise(tetheredGetWord);
}

new Promise(tetheredGetNumber)
    .then(determineParity, troubleWithGetNumber)
    .then(promiseGetWord)
    .then((info) => {
        console.log('Got: ', info.theNumber, ' , ', info.wordEvenOdd);
        return info;
    })
    .catch((reason) => {
        if (reason === -999) {
            console.error('Had previously handled error');
        } else {
            console.error(`Trouble with promiseGetWord(): ${reason}`);
        }
    })
    .finally((info) => console.log('All done'));
